<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    ChinaWanderer
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: RoleController.php 234 2011-04-07 03:29:01Z chang.zhenhua $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * role controller for backend
 * 
 * view,delete,search,sort,edit role
 * 
 * @package     Backend
 * @subpackage  Controller
 * @author      Chen Qiao <chen.qiao@evebit.com>
 * @version     $$Id: RoleController.php 234 2011-04-07 03:29:01Z chang.zhenhua $$
 */

class Default_RoleController extends Evebit_Controller_Action_Backend {
	
	/**
	 * @var $_moduleUrl string
	 */
	private $_moduleUrl = '';
	/**
	 * @var $_controllerUrl string
	 */
	private $_controllerUrl = '';
	
	/**
	 * default action of the role controller
	 * 
	 * view the role
	 * 
	 */
	public function indexAction() {
		$session = $this->getSession ();
		unset ( $session->grids ['filters'] );
		$session->initGrid ( 'role', array ('id' => array (), 'role_name' => array ('fields' => 'role_name', 'type' => 'string' ) ), array ('id' => 'id', 'role_name' => 'role_name' ), array ('size' => 10, 'sort' => 'id', 'order' => 'desc' ) );
		$event = new Default_Model_Entity_Role ();
		$collection = $event->getCollection ();
		$adapter = new Evebit_Model_Paginator_Adapter ( $collection );
		$paginator = new Zend_Paginator ( $adapter );
		$session->applyCollection ( $collection, $paginator );
		$this->view->collection = $collection;
		$this->view->paginator = $paginator;
		$this->view->state = $session->getGridState ();
	}
	
	/**
	 * reset action of the role controller
	 * 
	 * reset the role
	 * 
	 */
	public function resetAction() {
		$this->getSession ()->resetGrid ();
		$this->_forward ( 'index', $this->_controllerUrl );
	}
	
	/**
	 * add action of the role controller
	 * 
	 * add a role
	 * 
	 */
	public function addAction() {
		$this->_forward ( 'edit', null, null, array ('id' => 0 ) );
	}
	
	/**
	 * edit action of the role controller
	 * 
	 * edit a role,if the role is exist,show the role,
	 * else show a blank page to add a role
	 * 
	 */
	public function editAction() {
		$id = $this->getRequest ()->getParam ( 'id' );
		$role = new Default_Model_Entity_Role ();
		if ($id) {
			$role->load ( $id );
		}
		$acl = Zend_Registry::get ( "Acl" );
		$this->view->tree = $role->getRoleTree ();
		$this->view->acl = $acl;
		$this->view->event = $role;
	}
	
	/**
	 * saveEdit action of the role controller
	 * 
	 * when an administrator edited the role and the permission,save it
	 * 
	 */
	public function saveEditAction() {
		$data = $this->getRequest ()->getPost ( 'data' );
		$tree = $this->getRequest ()->getPost ( 'tree' );
		$acl = Zend_Registry::get ( "Acl" );
		if (! $acl->hasRole ( $data ["role_name"] )) {
			$acl->addRole ( new Zend_Acl_Role ( $data ["role_name"] ) );
		}
		$roleResource = new Default_Model_Entity_RoleResource ();
		$collection = $roleResource->getCollection ();
		$collection->addFieldToFilter ( "role_name", $data ["role_name"] );
		$items = $collection->getItems ();
		$treeAcl = explode ( "|", $tree );
		foreach ( $treeAcl as $treeItem ) {
			$treeArray = explode ( ",", $treeItem );
			$isExit = false;
			if (! $acl->has ( $treeArray [0] ))
				$acl->add ( new Zend_Acl_Resource ( $treeArray [0] ) );
			
			if ($treeArray [1] == "true") {
				$acl->allow ( $data ["role_name"], $treeArray [0] );
				foreach ( $items as $item ) {
					if ($item->getAllowResouce () == $treeArray [0])
						$isExit = true;
				}
				if (! $isExit) {
					$roleResource = new Default_Model_Entity_RoleResource ();
					$roleResource->setData ( "role_name", $data ["role_name"] );
					$roleResource->setData ( "allow_resouce", $treeArray [0] );
					$roleResource->save ();
				}
			
			} else {
				$acl->deny ( $data ["role_name"], $treeArray [0] );
				$isExit = false;
				foreach ( $items as $item ) {
					if ($item->getAllowResouce () == $treeArray [0])
						$isExit = true;
				}
				if ($isExit) {
					$roleResource = new Default_Model_Entity_RoleResource ();
					$collection = $roleResource->getCollection ();
					$collection->addFieldToFilter ( "role_name", $data ["role_name"] );
					$collection->addFieldToFilter ( "allow_resouce", $treeArray [0] );
					$roleResource = $collection->getFirstItem ();
					$roleResource->delete ();
				}
			}
		}
		
		$aclSerilize = new Default_Model_Entity_Acl ();
		$collection = $aclSerilize->getCollection ();
		if ($collection->count () > 0)
			$aclSerilize = $collection->getFirstItem ();
		$aclSerilize->setData ( "acl_serlize", serialize ( $acl ) );
		$role = new Default_Model_Entity_Role ();
		$role->addData ( $data );
		
		try {
			$result = $role->save ();
			$result = $aclSerilize->save ();
			$app = Evebit_Application::app ();
			$cacheManager = $app->getBootstrap ()->getResource ( 'cachemanager' );
			foreach ( $cacheManager->getCaches () as $cache ) {
				$cache->clean ( Zend_Cache::CLEANING_MODE_ALL );
			}
		} catch ( Exception $e ) {
			$this->getSession ()->addError ( $this->__ ( 'Save failed' ) );
			return $this->_forward ( 'edit', $this->_controllerUrl, $this->_moduleUrl, array ("id" => $data ["id"] ) );
		}
		$this->getSession ()->addSuccess ( $this->__ ( 'Save Success' ) );
		$this->_redirect ( $this->_controllerUrl . '/index' );
	}
	
	/**
	 * delete action of the role controller
	 * 
	 * delete one or more role
	 * 
	 */
	public function deleteAction() {
		$ids = $this->getRequest ()->getParam ( 'id' );
		if (count ( $ids ) > 0) {
			foreach ( $ids as $id ) {
				try {
					$role = new Default_Model_Entity_Role ();
					$role->load ( $id );
					if ($role->getId () != $id) {
						throw new Exception ( '' );
					}
					$role->delete ();
				} catch ( Exception $e ) {
					$this->getSession ()->addError ( $this->__ ( 'Role not found or deleted failed' ) );
				}
			}
			$this->getSession ()->addSuccess ( $this->__ ( 'Delete Success' ) );
		} else {
			$this->getSession ()->addNotice ( $this->__ ( 'Please select a record' ) );
		}
		$this->_redirect ( $this->_controllerUrl );
	}

}
